<?php

namespace Modules\Common\Exceptions;

use Dotenv\Exception\ValidationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Exceptions\HttpResponseException;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];
    public function render($request, Throwable $e)
    {

        if ($e instanceof BaseException) {
            return response()->json([
                'code'    => $e->getCode(),
                'message' => $e->getMessage(),
            ], $e->getHttpCode());
        }
        if ($e instanceof BusinessException) {
            return response()->json([
                'code'    => $e->getCode(),
                'message' => $e->getMessage(),
            ]);
        }
        if ($e instanceof ConfigException) {
            return response()->json([
                'code'    => $e->getCode(),
                'message' => $e->getMessage(),
            ], 500);
        }
        if ($e instanceof ModelNotFoundException) {
            return response()->json([
                'error' => 'Entry for '.str_replace('App\\', '', $e->getModel()).' not found'], 404);
        }




        return parent::render($request, $e); // TODO: Change the autogenerated stub
    }

    /**
     * Register the exception handling callbacks for the application.
     *
     * @return void
     */
    public function register()
    {
        $this->reportable(function (Throwable $e) {
            //
        });
    }
}
